package com.zhongke.cotrun.controller.manager;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Sets;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.common.util.AddIntegralUtils;
import com.zhongke.cotrun.model.pojo.UserInfo;
import com.zhongke.cotrun.model.pojo.UserInforCompany;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.UserAdminService;
import com.zhongke.cotrun.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RequestMapping("/manager/userManager")
@RestController
@Api(tags = "用户管理")
public class UserInfoManagerController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    UserAdminService userAdminService;
    @Autowired
    AddIntegralUtils addIntegralUtils;//积分工具类

    /**
     * 根据条件查询所有的
     *
     * @param userInforCompany
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectAllUserInfo")
    public APIResponse selectAllUserInfo(UserInforCompany userInforCompany, Integer pageNumber, Integer pageSize) {
        return APIResponse.returnSuccess(userInfoService.selectUserInfoAll(userInforCompany, pageNumber, pageSize));
    }

    /**
     * 查看具体的账号信息
     *
     * @param type
     * @param id
     * @return
     */
    @GetMapping("/selectAuthentication")
    public APIResponse selectAuthentication(@NotNull(message = "账户身份不能为空") @ApiParam(value = "类型 1:个人 2:机构 3:专家", required = true)Integer type,
                                            @NotNull(message = "账户id号不能为空") Integer id) {
        return APIResponse.returnSuccess(userAdminService.selecInformation(type, id));

    }


    /**
     * 批量修改审核状态
     *
     * @param ids
     * @param state
     * @return
     */
    @PostMapping("/upDateUserState")
    public APIResponse upDateUserState(
            @NotNull(message = "要修改的对象的id不能为空") String ids,
            @NotNull(message = "要修改的状态不能为空") Integer state) {

        String[] tem = ids.split(",");
        Integer[] array = new Integer[tem.length];
        for (int i = 0; i < tem.length; i++) {

            array[i] = Integer.parseInt(tem[i]);

        }

        Set<Integer> set = Sets.newHashSet(Arrays.asList(array));

        if (userInfoService.upUserState(set, state)) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.UPDATE_ERROR);
    }

    /**
     * 根据条件查询需要审核的用户
     *
     * @param userInfo
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/selectAuditUser")
    @ApiOperation(value = "根据条件查询需要审核的用户", notes = "根据条件查询需要审核的用户", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public APIResponse selectAuditUser(UserInfo userInfo, @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) {
        Map data = new HashMap(4);
        int count = userInfoService.queryUnApprovalCounts();
        PageInfo info = userInfoService.selectAuditUser(userInfo, pageNumber, pageSize);
        if (info.getList() != null) {
            data.put("count", count);
            data.put("data", info);
            return APIResponse.returnSuccess(data);
        }
        return APIResponse.returnFail("暂无数据");
    }

    /**
     * 根据类型和id查询详情
     *
     * @param id           待审核信息的id,
     * @param approvalType
     * @return
     */
    @GetMapping("/getUserInfoById")
    @ApiOperation(value = "根据类型和id查询详情", notes = "根据类型和id查询详情", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public APIResponse getUserInfoById(@ApiParam(value = "主键id", required = true) Long id, @ApiParam(value = "申请类型 1:专家 2:机构", required = true) Integer approvalType) {
        return APIResponse.returnSuccess(userInfoService.selectAuditUserInfoById(id, approvalType));
    }

    /**
     * 根据类型审核通过用户信息
     *
     * @param id
     * @param approvalType
     * @return
     */
    @PostMapping("/approvalPass")
    @ApiOperation(value = "根据类型审核通过用户信息", notes = "根据类型审核通过用户信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public APIResponse approvalPass(@ApiParam(value = "主键id", required = true) Long id, @ApiParam(value = "对应用户ID", required = true) Integer linkId, @ApiParam(value = "申请类型 1:专家 2:机构", required = true) Integer approvalType, HttpSession session) throws ParseException {
        if (userInfoService.approvalPass(id, linkId, approvalType)) {
//用户积分生效
            UserInfo userInfo1 = userInfoService.selectUserInfoByid(linkId);

            Map m = addIntegralUtils.getMapOfUserEntity(userInfo1, 0, "/manager/userManager/approvalPass",
                    "VIP0", "用户信息审核的时候增加:", session);
            Map m1 = addIntegralUtils.getMapOfUserEntity(userInfo1, 0, "/manager/userManager/ExpertInfo",
                    "VIP0", "用户信息审核的时候增加:", session);
            return APIResponse.returnSuccess("操作成功");
        }


        return APIResponse.returnFail("操作失败");
    }

    /**
     * 根据类型审核驳回用户信息
     *
     * @param id
     * @param approvalType
     * @return
     */
    @PostMapping("/approvalReject")
    @ApiOperation(value = "根据类型审核驳回用户信息", notes = "根据类型审核驳回用户信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public APIResponse approvalReject(@ApiParam(value = "主键id", required = true) Long id,
                                      @ApiParam(value = "申请类型 1:专家 2:机构", required = true) Integer approvalType,
                                      @ApiParam(value = "审核意见", required = true) String auditOpinion) {
        if (userInfoService.approvalReject(id, approvalType, auditOpinion))
            return APIResponse.returnSuccess("操作成功");
        return APIResponse.returnFail("操作失败");
    }

}
